import type { NextApiRequest, NextApiResponse } from "next";

const os = require("os");

type BootTimeData = {
  time: string;
};

async function getAsyncBootInfo(): Promise<string> {
  let time: string;

  let handler = new Promise<string>(resolve => {
    setTimeout(() => {
      resolve(JSON.stringify(os.uptime()));
    });
  });
  time = await handler;
  return time;
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<BootTimeData>
) {
  res.status(200).json({ time: await getAsyncBootInfo() });
}
